import cv2
import numpy as np

if __name__ == '__main__':
    # 1. 图片输入
    image_np = cv2.imread('work2.jpg')
    image_hsv = cv2.cvtColor(image_np, cv2.COLOR_BGR2HSV)
    # _, image_thresh = cv2.threshold(image_gray, 192, 255, cv2.THRESH_BINARY)
    g_min = (0, 0, 46)
    g_max = (180, 43, 220)
    mask = cv2.inRange(image_hsv, g_min, g_max)
    mask = cv2.bitwise_not(mask)
    conunters, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # print(conunters)

    for cnt in conunters:
    # print(cnt.shape)
    # print(cnt)
        hull = cv2.convexHull(cnt)
        print(hull.shape)
        x, y, z = map(int, np.random.randint(0, 256, 3))
        cv2.polylines(image_np, [hull], isClosed= True, color=(x, y, z), thickness= 2)

        # print(hull[j])
    # cv2.polylines(image_np, )

    cv2.imshow('', image_np)
    cv2.imwrite('zuoye2.jpg', image_np)
    cv2.waitKey(0)